package com.bytedance.scenedemo.util;

import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.photokit.metadata.AVStorage;
import ohos.utils.net.Uri;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;

/**
 * @author rpf
 * @since 2021-06-01
 */
public class ImageUtil {
    public static HiLogLabel logLabel = new HiLogLabel(HiLog.LOG_APP, 0x001, ImageUtil.class.getSimpleName());

    /**
     * uri to pixmap
     *
     * @param context mContext
     * @param path    uri
     * @return pixelMap
     */
    public static PixelMap getPixelMap(Context context, Uri path) {
        PixelMap pixelMap = null;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        ImageSource.SourceOptions options = new ImageSource.SourceOptions();
        try {
            FileDescriptor fileDescriptor = helper.openFile(path, "r");
            ImageSource source = ImageSource.create(fileDescriptor, options);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            pixelMap = source.createPixelmap(decodingOptions);

        } catch (DataAbilityRemoteException e) {
            HiLog.error(logLabel, e.getMessage());
        } catch (FileNotFoundException e) {
            HiLog.error(logLabel, e.getMessage());
        }
        return pixelMap;
    }

    public static PixelMap getPixelMap2(Context context, String chooseImgUri) {
        PixelMap pixelMap = null;

        //定义数据能力帮助对象
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        //定义图片来源对象
        ImageSource imageSource = null;
        //获取选择的Img对应的Id
        String chooseImgId = null;
        //如果是选择文件则getUriString结果为content://com.android.providers.media.documents/document/image%3A30，其中%3A是":"的URL编码结果，后面的数字就是image对应的Id
        //如果选择的是图库则getUriString结果为content://media/external/images/media/30，最后就是image对应的Id
        //这里需要判断是选择了文件还是图库
        if (chooseImgUri.lastIndexOf("%3A") != -1) {
            chooseImgId = chooseImgUri.substring(chooseImgUri.lastIndexOf("%3A") + 3);
        } else {
            chooseImgId = chooseImgUri.substring(chooseImgUri.lastIndexOf('/') + 1);
        }
        //获取图片对应的uri，由于获取到的前缀是content，我们替换成对应的dataability前缀
        Uri uri = Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, chooseImgId);
        System.out.println("TAG---" + uri.toString());
        try {
            //读取图片
            FileDescriptor fd = helper.openFile(uri, "r");
            imageSource = ImageSource.create(fd, null);
            //创建位图
            pixelMap = imageSource.createPixelmap(null);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (imageSource != null) {
                imageSource.release();
            }
        }
        return pixelMap;

    }


}
